package com.mls.fivedistrict.controller.backend;


import com.baomidou.mybatisplus.mapper.Condition;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.ApplicationClassify;
import com.mls.fivedistrict.server.ApplicationClassifyService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Five Team
 * @since 2018-06-04
 */
@RestController("backendApplicationClassifyController")
@RequestMapping("/api/manager")
public class ApplicationClassifyController extends BaseController {

    @Autowired
    private ApplicationClassifyService applicationClassifyService;

    /**
     * 应用分类列表
     * @return
     */
    @GetMapping("/v1/applicationClassifys")
    public ServiceResponse getApplicationClassifyList() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List<ApplicationClassify> applicationClassifyList = applicationClassifyService.selectList(pageFilter.getWrapper());
        PageInfo pageInfo = new PageInfo(applicationClassifyList, 5);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }

    /**
     * 单个应用分类详情查询
     * @param applicationClassifyId
     * @return
     */
    @GetMapping("/v1/applicationClassify/{applicationClassifyId}")
    public ServiceResponse getApplicationClassify(@PathVariable Long applicationClassifyId) {
        Optional.ofNullable(applicationClassifyId).orElseThrow(() -> new CustomerException("分类id不能为空"));
        ApplicationClassify applicationClassify = applicationClassifyService.selectById(applicationClassifyId);
        if(null!=applicationClassify){
            return ServiceResponse.createSuccessResponse(applicationClassify);
        }
        return ServiceResponse.createErrorResponse("查无此应用");
    }

    /**
     * 应用分类新增
     * @param applicationClassify
     * @return
     */
    @PostMapping("/v1/applicationClassify")
    public ServiceResponse insert(@RequestBody ApplicationClassify applicationClassify) {
        BeanValidator.check(applicationClassify);
        boolean insert = applicationClassifyService.insert(applicationClassify);
        if (!insert) {
            return ServiceResponse.createErrorResponse("添加失败");
        }
        return ServiceResponse.createSuccessResponse("添加成功");
    }

    /**
     * 应用分类修改
     * @param applicationClassifyId
     * @param applicationClassify
     * @return
     */
    @PutMapping("/v1/applicationClassify/{applicationClassifyId}")
    public ServiceResponse update(@PathVariable Long applicationClassifyId, @RequestBody ApplicationClassify applicationClassify) {
        Optional.ofNullable(applicationClassifyId).orElseThrow(() -> new CustomerException("分类id不能为空"));
        Optional.ofNullable(applicationClassify).orElseThrow(() -> new CustomerException("分类应用不能为空"));
        applicationClassify.setId(applicationClassifyId);
        boolean update = applicationClassifyService.updateById(applicationClassify);
        if (!update) {
            return ServiceResponse.createErrorResponse("更新失败");
        }
        return ServiceResponse.createSuccessResponse("更新成功");
    }

    /**
     * 应用分类删除
     * @param applicationClassifyId
     * @return
     */
    @DeleteMapping("/v1/applicationClassify/{applicationClassifyId}")
    public ServiceResponse delete(@PathVariable Long applicationClassifyId){
        Optional.ofNullable(applicationClassifyId).orElseThrow(() -> new CustomerException("分类id不能为空"));
        ApplicationClassify applicationClassify = new ApplicationClassify();
        applicationClassify.setId(applicationClassifyId);
        applicationClassify.setDelete(true);
        boolean delete = applicationClassifyService.updateById(applicationClassify);
        if(delete){
            return ServiceResponse.createSuccessResponse("删除成功");
        }else{
            return  ServiceResponse.createErrorResponse("删除失败");
        }
    }
}

